<?php
namespace app\app\validate;

use app\common\validate\Login as ParentValidate;

class Login extends ParentValidate {
	/**
     * 验证规则
     */
    protected $rule = [
    	'username' => 'require|length:4,32',
    	'password' => 'require|length:4,32|password'
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'username.require'  => '用户名不能为空',
    	'username.length'   => '用户名必须在4到32个字符之间',
    	'password.require'  => '密码不能为空',
    	'password.length'   => '密码长度必须在4到32个字符之间',
    	'password.password' => '密码错误'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
    	'login' => ['username', 'password']
    ];

    //验证密码是否正确
    protected function password($value, $rule, $data = []) {
        $passwordTool = loadTool('Password');
        $row = loadOnly('tyyh\\User')->find4username($data['username']);
        if(empty($row)) {
            return false;
        }
        return $passwordTool->encryption($value, $row['salt']) == $row['password'];
    }
}